package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.SpuImage;
import com.atguigu.gmall.product.entity.SpuInfo;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.vo.SpuInfoVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("admin/product")
@RestController
public class SpuManageController {
    @Autowired
    SpuInfoService spuInfoService;

    // http://weiranyi.xiaomy.net/admin/product/1/10?category3Id=2
    @GetMapping("/{page}/{limit}")
    public Result spuList(
            @PathVariable("page") Long pn,
            @PathVariable("limit") Long ps,
            @RequestParam("category3Id") Long c3id
    ) {
        Page<SpuInfo> result = spuInfoService.getSpuList(pn, ps, c3id);
        return Result.ok(result);
    }

    // 保存spu
    // http://weiranyi.xiaomy.net/admin/product/saveSpuInfo
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfoVo spuInfoVo) {
        spuInfoService.saveSpuInfo(spuInfoVo);
        return Result.ok();
    }

    // 查询图片列表
    @GetMapping("/spuImageList/{spuId}")
    public Result spuImageList(@PathVariable("spuId") Long spuId) {
        List<SpuImage> spuImages = spuInfoService.getSpuImageList(spuId);
        return Result.ok(spuImages);
    }

    // 根据spuId获取销售属性
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result getSpuSaleAttrList(@PathVariable("spuId") Long spuId) {
        List<SpuSaleAttr> spuSpuSaleAttrs = spuInfoService.getSpuSaleAttrList(spuId);
        return Result.ok(spuSpuSaleAttrs);
    }
}
